<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:Clickr 訂單信息
 *@param datetime 2012-01-12
 *@param author abin
 ********************/
class Promotion extends Admins_Controller {
  
  private $data=array();

  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='orders';
    $this->data['format_time']=$this->settings['default_time'];
    $this->load->model('orders_mdl');
	$this->data['lang'] =$this->lang->language;
  }

  public function index(){
    redirect("admins/orders");
  }

  //訂單剔選后的推廣
  public function order(){
    $this->data['phone_data']=$this->_get_order_list();
    $this->data['redirect']='orders';
    $this->load->view('admins/promotion_form_view',$this->data);
  }

  //獲取訂單手機號碼列表
  private function _get_order_list(){
    $promotion_string=$this->input->get('promotion_string');
    $phone_data=array();
    if(!empty($promotion_string)){
      $order_data=explode(',',$promotion_string);
      if(!empty($order_data) && is_array($order_data)){
        $query=$this->orders_mdl->get_orders(array('id_array'=>$order_data));
        if(!empty($query) && $query->num_rows()>0){
          foreach ($query->result_array() as $item) {
            if(!empty($item['phone']) && strlen($item['phone'])==8 && substr($item['phone'],0,1)=='6'){
              array_push($phone_data,$item['phone']);
            }
          } // end foreach;
        }
      } // end if order_data
    }
    return !empty($phone_data)?array_unique($phone_data):'';
  }

  //積分列表剔選后推廣
  public function integral(){
    $this->data['phone_data']=$this->_get_integral_list();
    $this->data['redirect']='integral';
    $this->load->view('admins/promotion_form_view',$this->data);
  }

  //獲取積分手機號碼 _get_integral_list
  private function _get_integral_list(){
    $promotion_string=$this->input->get('promotion_string');
    $phone_data=array();
    if(!empty($promotion_string)){
      $order_data=explode(',',$promotion_string);
      $this->load->model('integral_mdl');
      if(!empty($order_data) && is_array($order_data)){
        $query=$this->integral_mdl->get_integral(array('id_array'=>$order_data));
        if(!empty($query) && $query->num_rows()>0){
          foreach ($query->result_array() as $item) {
            if(!empty($item['phone']) && strlen($item['phone'])==8 && substr($item['phone'],0,1)=='6'){
              array_push($phone_data,$item['phone']);
            }
          } // end foreach;
        }
      } // end if order_data
    }
    return !empty($phone_data)?array_unique($phone_data):'';
  }


  //執行短信推廣
  public function modify(){
    $phone_data=$this->input->post('phone_data');
    $new_number_list=$this->input->post('new_number_list');
    $comment=$this->input->post('comment');
    $target=$this->input->post('redirect');
    if((!empty($phone_data) || !empty($new_number_list)) && !empty($comment)){
      $phone_data=!empty($phone_data)?unserialize($phone_data):array();//解除序列化
      //組合新號碼
      $number_list_array=!empty($new_number_list)?explode(',',$new_number_list):array();
      $phone_data=array_merge($phone_data,$number_list_array);

      array_unique($phone_data);//刪除重複號碼

      $phone_count=count($phone_data);
      //獲取短信剩餘條數
      $setting=array(
        'group'=>'other',
        'key'=>'sms_count'
      );
      $query=$this->settings_mdl->get_settings_by_where($setting);
      $total_sms=$query['value'];
      if(($total_sms-$phone_count)>0){ //判斷短信條數
        $phone_number=implode(',',$phone_data);
        $send_result=helper_send_macau_sms($phone_data,$comment);//發送短信
        $this->load->model('sms_mdl');
        foreach ($phone_data as $item) {
          $send_data=array(
            'phone_number'=>$item,
            'message_txt'=>$comment,
            'send_result'=>$send_result,
            'phone_area'=>'推廣',
            'send_date'=>time(),
            'add_time'=>time(),
            'update_time'=>time(),
          );
          $this->sms_mdl->add_sms($send_data);
        }
        $deduct=$total_sms-$phone_count;
        $this->_sms_count_modify($send_result,$deduct);
        $this->operator_log('進行短信推廣,推廣號碼:'.$phone_number,'短信推廣',1);
        $msg = $send_result=='發送成功'?'恭喜,您的短信推廣操作成功!':'對不起,您的操作失敗';
        $notify = $send_result=='發送成功'?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        $redirect=site_url('admins/'.$target).$this->settings['url_query'];//獲取列表篩選參數
        redirect($redirect);
      }else{
        $success_message=$this->settings['title'].'==短信餘額不足(後台短信推廣)';
        $content='<p>您好,您的短信餘額不足,請及時充值：</p>';
        $content.='<p>剩餘短信條數:<span style="color:red">0</span></p>';
        $content.='<p>詳情聯絡Clickr 力嘉電話:+(853) 2823 1274</p>';
        $content.='<p>或瀏覽網站:<a href="http://clickrweb.com">http://clickrweb.com</a></p>';
        $content.='<p>發送時間:'.date('Y/m/d,H:i')."</p>";
        $this->_send_email($success_message,$content,'info@clickrweb.com'); //發送給店主
        show_error('對不起,您的短信餘額不足,不能發送短信推廣,請確認短信剩餘條數!');
        exit;
      }
    }else{
      show_error('對不起,推廣號碼為空,或沒有短信內容!');
      exit;
    }
  }

  //推送email通知
  private function _send_email($success_message='',$content='',$cc=''){
    $protocol= $this->settings['default_email_type']; //email發送方法
    $email_to= $this->settings['email_to']; //email接收者
    $email_host= $this->settings['email_host']; //Email發出者
    $email_form= $this->settings['email_form']; //發出者名字
    $smtp_host= $this->settings['smtp_host']; //smtp地址smtp_password
    $smtp_password= $this->settings['smtp_password']; //賬戶密碼
    $smtp_port= $this->settings['smtp_port']; //服務器端口

    header('Content-Type: text/html; charset=UTF-8');
    $config['protocol']=($protocol==1)?'mail':'smtp';
    if($protocol==2){
      $config['smtp_host']    = $smtp_host;  
      $config['smtp_user']    = $email_host;  
      $config['smtp_pass']    = $smtp_password;  
      $config['smtp_port']    = $smtp_port;
      $config['smtp_timeout'] = '5';
    }
    $config['charset']      = 'utf-8';  
    $config['mailtype']     = 'html';
    $config['newline'] = "\r\n";
    $this->load->library('email',$config);
    $this->email->from($email_host,$email_form);
    $this->email->to($email_to);
    if(!empty($cc)){
      $this->email->cc($cc);
    }
    $this->email->subject($success_message);
    $this->email->message($content);
    return $this->email->send();
  }

  //扣除短信數
  //如果短信數不夠 send email提示
  private function _sms_count_modify($send_result,$deduct){
    if(!empty($send_result) && $send_result=='發送成功'){
      $setting=array(
        'group'=>'other',
        'key'=>'sms_count',
        'value'=>$deduct
      );
      $this->settings_mdl->edit_settings_by_keyGroup($setting);
    }
    //獲取短信數不夠提示的條數
    $setting=array(
      'group'=>'other',
      'key'=>'sms_remind'
    );
    $query=$this->settings_mdl->get_settings_by_where($setting);
    $sms_remind=$query['value'];
    //如果短信數不夠 send email提示
    if($deduct<=$sms_remind){
      $success_message=$this->data['title'].'==短信餘額不足(後台短信推廣)';
      $content='<p>您好,您的短信餘額不足,請及時充值：</p>';
      $content.='<p>剩餘短信條數:<span style="color:red">'.$deduct."</span></p>";
      $content.='<p>詳情聯絡Clickr 力嘉電話:+(853) 2823 1274</p>';
      $content.='<p>或瀏覽網站:<a href="http://clickrweb.com">http://clickrweb.com</a></p>';
      $content.='<p>發送時間:'.date('Y/m/d,H:i')."</p>";
      $this->_send_email($success_message,$content,'info@clickrweb.com'); //發送給店主
    }
  }
}